package com.sdp.wx.task;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.flow.db.service.IActRuTaskService;
import com.sdp.flow.dto.TodoTaskView;
import com.sdp.right.cron.ScheduledOfTask;
import com.sdp.right.websocket.dto.MessageInfo;
import com.sdp.right.websocket.server.MessageWebsocketServer;
import com.sdp.wx.utils.WxConstants;




@Component
public class FlowTodoTask implements ScheduledOfTask{
	
	@Autowired
	private MessageWebsocketServer messageWebsocketServer;


	@Autowired
	private IActRuTaskService ruTaskService;
	
	@Autowired
	private Query query;
	
	@Value("${sdp.systemNo}")
	private String systemNo;
	

	public void execute() {
		MessageInfo messageInfo = null;
		Map<String, Map<String, String>> empClientIdMap = MessageWebsocketServer.getEmpClientIdMap();
		if(empClientIdMap==null || empClientIdMap.size()==0) {
			return;
		}
		
		Map<String,BigDecimal> todoCountMap = new HashMap<>();
		//查询所有待办的任务信息
		QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("c.SYSTEM_NO", systemNo);
		List<TodoTaskView> li = query.viewList(ruTaskService, TodoTaskView.class, null, queryWrapper);
		if(li==null || li.size()==0) {
			for (Entry<String, Map<String, String>> entry : empClientIdMap.entrySet()) {
				messageInfo = new MessageInfo();
				messageInfo.setData(BigDecimal.ZERO);
				messageInfo.setEmpNo(entry.getKey());
				messageInfo.setType(WxConstants.websocket待审批数量.getCode());
				messageWebsocketServer.sendMessage(messageInfo);
			}
			return;
		}
		
		for(TodoTaskView t : li) {
			if(todoCountMap.containsKey(t.getAssignee())) {
				todoCountMap.put(t.getAssignee(), todoCountMap.get(t.getAssignee()).add(BigDecimal.ONE));
			}else {
				todoCountMap.put(t.getAssignee(),BigDecimal.ONE);
			}
		}
		
		for (Entry<String, Map<String, String>> entry : empClientIdMap.entrySet()) {
			if(todoCountMap.containsKey(entry.getKey())) {
				messageInfo = new MessageInfo();
				messageInfo.setData(todoCountMap.get(entry.getKey()));
				messageInfo.setEmpNo(entry.getKey());
				messageInfo.setType(WxConstants.websocket待审批数量.getCode());
				messageWebsocketServer.sendMessage(messageInfo);
			}else {
				messageInfo = new MessageInfo();
				messageInfo.setData(BigDecimal.ZERO);
				messageInfo.setEmpNo(entry.getKey());
				messageInfo.setType(WxConstants.websocket待审批数量.getCode());
				messageWebsocketServer.sendMessage(messageInfo);
			}
		}
		
	}
}
